﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame2
{
    class Textbox
    {
        public string text;
        public Rectangle area;
        public bool selected;
        public float charTimer;
        private char previous;
        private float delay = 500;

        public Textbox(int x, int y)
        {
            area = new Rectangle(x, y, 180, 30);
            selected = false;
            charTimer = 0;
            previous = (char)0;
            text = "";
        }

        public bool detectClick(int x, int y)
        {
            Rectangle click = new Rectangle(x, y, 1, 1);
            if (click.Intersects(area))
            {
                return true;
            }
            return false;
        }

        public void draw(SpriteBatch spriteBatch, Texture2D texture, SpriteFont font)
        {
            if (selected)
            {
                spriteBatch.Draw(texture, new Rectangle(area.X - 2, area.Y - 2, area.Width + 4, area.Height + 4), Color.Red);
                spriteBatch.Draw(texture, new Rectangle(area.X, area.Y, area.Width, area.Height), Color.White);
                spriteBatch.DrawString(font, text, new Vector2(area.X + 4, area.Y + 2), Color.Black);
            }
            else
            {
                spriteBatch.Draw(texture, new Rectangle(area.X - 1, area.Y - 1, area.Width + 2, area.Height + 2), Color.Black);
                spriteBatch.Draw(texture, new Rectangle(area.X, area.Y, area.Width, area.Height), Color.White);
                spriteBatch.DrawString(font, text, new Vector2(area.X + 4, area.Y + 2), Color.Black);
            }
        }

        public void update(GameTime gameTime)
        {
            if (returnChar() != (char)0 && returnChar() != previous)
            {
                char c = returnChar();
                if (c == (char)8)
                {
                    if (text.Length > 0)
                    {
                        text = text.Remove(text.Length - 1);
                        previous = c;
                    }
                }
                else
                {
                    text += c;
                    previous = c;
                }
            }

            if (charTimer > delay)
            {
                previous = (char)0;
                charTimer = 0;
            }
            charTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
        }

        private char returnChar()
        {
            Keys[] keys = Keyboard.GetState().GetPressedKeys();
            KeyboardState keyState = Keyboard.GetState();

            for (int i = 0; i < keys.Length; i++)
            {
                int key = (int)keys[i];

                if (key == 8)
                {
                    return (char)8;
                }

                if (keyState.IsKeyDown(Keys.LeftShift) || keyState.IsKeyDown(Keys.RightShift))
                {
                    if (key > 64 && key < 91)
                        return (char)key;
                }
                else
                {
                    if (key > 31 && key < 127)
                    {
                        if (key > 64 && key < 91)
                        {
                            return (char)(key + 32);
                        }
                        return (char)key;
                    }
                }

                if (keyState.IsKeyDown(Keys.OemMinus))
                {
                    return (char)95;
                }
            }
            return (char)0;
        }
    }
}
